import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export  const createStore = ()=>{

    return  new Vuex.Store({
        state:()=>({
            posts:[]
        }),
        mutations:{
            setPosts(state,data){
                state.posts = data
            }
        },
        actions:{
            // 服务端渲染，需要返回 promise，需要等待执行之后再调用
            async getPost({commit}){
                const { data } = await axios({method: 'GET',url: 'https://cnodejs.org/api/v1/topics'})
                commit('setPosts',data.data)
            }
        }
    })
}


